package com.work.saturn.account;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.immo.libcommon.base.BaseActivity;
import com.immo.libcommon.base.LoginBean;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.constant.SharePreferenceKey;
import com.immo.libcommon.http.NetworkRequest;
import com.immo.libcommon.utils.SharePreferenceUtils;

import com.work.saturn.R;
import com.immo.libcommon.constant.RouterUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.immo.libcommon.utils.InputValidationUtils.inputIsEmpty;

/**
 * @author Administrator
 * @content 登录页面
 * @date 2018/7/10
 */
@Route(path = RouterUtil.LOGIN_PAGE)
public class LoginActivity extends BaseActivity {
    /**
     * 用户名
     */
    @BindView(R.id.login_input_username)
    EditText userName;
    /**
     * 密码
     */
    @BindView(R.id.login_input_password)
    EditText password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //禁止进入时弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        ButterKnife.bind(this);
    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        return inputIsEmpty(this, userName.getText().toString(), getString(R.string.username_no_empty))
                && inputIsEmpty(this, password.getText().toString(), getString(R.string.password_no_empty));
    }


    /**
     * 登录成功，将用户名和token存储
     *
     * @param token 用户的登录的Token
     */
    private void loginSuccess(String token) {
        toast(getString(R.string.login_success));
        //存储用户名和token
        SharePreferenceUtils.put(LoginActivity.this, SharePreferenceKey.USERNAME, userName.getText().toString());
        SharePreferenceUtils.put(LoginActivity.this, SharePreferenceKey.TOKEN, token);
        //跳到首页
        ARouter.getInstance().build(RouterUtil.MAIN_PAGE).withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation();
        finish();
    }


    /**
     * 登录接口请求
     */
    private void LoginNet() {
        HashMap<String, String> params = new HashMap<>();
        params.put("username", userName.getText().toString());
        params.put("password", password.getText().toString());
        new NetworkRequest((object, data) -> {
            if (object instanceof LoginBean) {
                LoginBean loginBean = (LoginBean) object;
                loginSuccess(loginBean.getPayload().getToken());
            }
        }).requestPost(BaseUrl.LOGIN, this, JSON.toJSONString(params), LoginBean.class, true, false);
    }

    @OnClick({R.id.login_back, R.id.login_submit, R.id.login_to_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_back:
                //返回
                finish();
                break;
            case R.id.login_submit:
                //提交
                if (isCheck()) {
                    LoginNet();
                }
                break;
            case R.id.login_to_register:
                //跳到注册
                ARouter.getInstance().build(RouterUtil.REGISTER_PAGE).navigation();
                break;
        }
    }
}
